package com.ag.lzw.common;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.bean.TbTask;
import com.ag.lzw.dao.LzwTaskDAO;
import com.ag.lzw.vo.TaskTreeVo;

/**
 * 任务树的操作类
 * @author zhiweiofli
 *
 */
@Service("taskTreeManager")
public class TaskTreeManager {
	private static Logger logger = Logger.getLogger(TaskTreeManager.class);
	
	private static TaskTreeManager taskTreeManager;

	@Resource(name = "lzwTaskDao")
	private LzwTaskDAO taskDAO;
	
	private TaskTreeManager() {

	}

	public static synchronized TaskTreeManager getInstance() {
		if (taskTreeManager == null) {
			taskTreeManager = new TaskTreeManager();
		}
		return taskTreeManager;
	}


	/**
	 * 由任务ID，获取直属子任务
	 * @param task 
	 *             TbTask
	 * @return Set<TbTask> 子任务的集合
	 */
	public Set<TbTask> getChildTask(String id){
		logger.info("finding task by id:"+id);
		List list = this.taskDAO.findTasksById(id);
		if(list == null && list.size() == 0 ){
			logger.info("Cannot found the task by id:"+id);
			return null;
		} else {
			TbTask tt = (TbTask)list.get(0);
			return tt.getTbTasks();
		}
	}
	
	/**
	 * 有事件ID获取属于该事件的整个任务树
	 * @param Eid 
	 *            String
	 * @return 
	 *         TaskTreeVo
	 */
	public Set<TaskTreeVo> getTaskTree(String Eid) {
		Set<TaskTreeVo> taskTreeSet = null;
		logger.info("finding tasks by eid:"+Eid);
		List<TbTask> etask = this.taskDAO.findTaskByEid(Eid);  //按事件ID查询任务的实体
		logger.info("the result's length is:"+etask.size());
		if(etask != null && etask.size()!=0){
			taskTreeSet = new HashSet<TaskTreeVo>(0);  //存在与事件相关的任务，初始化参数
			
			Iterator iterator = etask.iterator();
			while(iterator.hasNext()){                 //遍历与事件相关的任务集合
				String tid = ((TbTask)iterator.next()).getTId();
				logger.info("finding task by id:"+tid);
				List list = this.taskDAO.findTasksById(tid); //
				if(list!=null && list.size()>0){
					TbTask ttask = (TbTask) list.get(0);
					taskTreeSet.add( getChildenTasks(ttask) );  //把该任务的子任务集合保存在set中
				} else {
					logger.info("Cannot found the task by id:"+tid);
					taskTreeSet = null; //查询失败，置空参数，释放内存
					return null;
				}
			}
			return taskTreeSet;
		} else {
			logger.info("Cannot found the task of EventID:"+Eid);
			return null;
		}
	}
	
	/**
	 * 根据任务ID，获取其属下所有的子任务
	 * @param id
	 *           String
	 * @return 以树形结构返回结构
	 *                        TaskTreeVo
	 */
	public TaskTreeVo getChildrenTaskTree(String id) {
		logger.info("finding task by id:"+id);
		List nodes = this.taskDAO.findTasksById(id);
		if(nodes == null && nodes.size() == 0 ){
			logger.info("Cannot found the task by id:"+id);
			return null;
		} else {
			TaskTreeVo ttv = null;
			for(Object node : nodes){
				ttv = getChildenTasks((TbTask)node);
			}
			//TbTask node = (TbTask)nodes.get(0);
			return ttv;
		}
	}

	/**
	 * 获取其所有的子任务
	 * @param node 抓取了TbTask属性tbtasks的实体
	 *             TbTask
	 * @return treeNode
	 *             TaskTreeVo
	 */
	public TaskTreeVo getChildenTasks(TbTask node) {
		
		TaskTreeVo treeNode = new TaskTreeVo(); 
		
		if (node.getTbTasks() != null && node.getTbTasks().size() != 0) {
			Iterator<TbTask> iterator = node.getTbTasks().iterator();
			while (iterator.hasNext()) {
				
				TbTask nodeChild = iterator.next();
				
				TbTask taskNode = (TbTask)this.taskDAO.findTasksById(nodeChild.getTId()).get(0);
				
				treeNode.getChildrenNode().add(getChildenTasks(taskNode));
			}
			treeNode.setNode(node);
			return treeNode;
		} else {
			treeNode.setNode(node);
			treeNode.setChildrenNode(null);
			return treeNode;
		}
	}

}
